import { useData } from "@/components";
import { FormContext } from "@/components/Form/Context";
import { useContext, useEffect } from "react";

/**
 * 用于更新在form表单下的输入框更新
 * @param name 
 * @param value 
 * @param oldValue 
 */
export const useUpdateForm = (
  name: string,
  value: unknown,
  oldValue: unknown,
) => {
  const formContent = useContext(FormContext);
  const innerMethods = formContent.store?.getInnerMethods()
  useEffect(() => {
    if (formContent.inForm) {
      innerMethods?.innerSetFieldValue(name, value)
      const newValue = { [name]: value }
      const fields = formContent.store?.getFields()
      const oldObj = { [name]: oldValue }

      formContent?.onChange?.(
        newValue,
        fields,
        oldObj
      )
      formContent?.onValuesChange?.(
        newValue,
        fields,
        oldObj
      )
    }
  }, [value])
}